// app/controller/users.js
const Controller = require('../controller/baseController')
const { Op } = require('sequelize')

class ArticalController extends Controller {
	// 获取所有的列表不分页
	async listAll() {
		const { ctx } = this
		let options = {
			distinct: true,
			include: [
				{
					model: this.ctx.model.Categorical,
					attributes: ['id', 'name', 'remark'],
					where: {
						status: true,
					},
				},
			],
		}
		const data = await ctx.service.artical.listAll(options)

		ctx.helper.success({ data })
	}
	// 获取所欲的文章列表
	async list() {
		const { ctx } = this
		const {
			query: { pageSize = 8, currentPage = 1 },
			helper,
		} = ctx
		let queryArr = {
			limit: helper.toInt(pageSize),
			offset: (helper.toInt(currentPage) - 1) * helper.toInt(pageSize),
			distinct: true,
			include: [
				{
					model: this.ctx.model.Categorical,
					attributes: ['id', 'name', 'remark'],
					where: {
						status: true,
					},
				},
			],
		}
		queryArr = Object.assign(queryArr, { order: [['created_at', 'DESC']] })

		const data = await ctx.service.artical.list(queryArr)

		helper.success({ data })
	}
	// 根据文章名称模糊查询
	async findLike() {
		const { ctx } = this
		const {
			query: { category, title, begin, end, pageSize = 8, currentPage = 1 },
			helper,
		} = ctx
		let where = {}
		if (begin && end) {
			where['createdAt'] = {
				[Op.between]: [ctx.query.begin, ctx.query.end],
			}
		}
		let relative = {}
		if (category) {
			relative['remark'] = {
				[Op.eq]: category,
			}
		}
		let options = {
			limit: helper.toInt(pageSize),
			offset: (helper.toInt(currentPage) - 1) * helper.toInt(pageSize),
			order: [['created_at', 'DESC']],
			where: {
				title: {
					[Op.substring]: title,
				},
				...where,
			},
			include: [
				{
					model: this.ctx.model.Categorical,
					attributes: ['id', 'name', 'remark'],
					where: {
						status: true,
						...relative,
					},
				},
			],
		}
		const data = await ctx.service.artical.findLike(options)
		helper.success({ data })
	}
	// 添加文章
	async add() {
		const { ctx } = this
		const { title, content } = ctx.request.body
		const { helper } = ctx
		if (!title || !title.trim()) {
			return helper.fail({ msg: '标题不能够为空！' })
		}
		if (!content || !content.trim()) {
			return helper.fail({ msg: '内容不能够为空！' })
		}
		let options = {
			where: {
				title,
			},
		}
		const res = await ctx.service.artical.add(ctx.request.body, options)
		if (res.flag) {
			helper.success({ data: res.data.dataValues, msg: '添加成功' })
		} else {
			helper.fail({ msg: res.msg })
		}
	}

	// 删除文章
	async del() {
		const { ctx } = this
		let { ids } = ctx.request.body
		const { helper } = ctx
		const res = await ctx.service.artical.del(ids)
		if (res.flag) {
			helper.success({ msg: '删除成功' })
		} else {
			helper.fail({ msg: res.msg })
		}
	}
	// 修改文章

	async update() {
		const { ctx } = this
		let {
			request: { body },
			helper,
		} = ctx
		if (!body.id) return helper.fail({ msg: '文章id必填字段！' })

		const odl = await ctx.model.Artical.findByPk(ctx.request.body.id)

		if (!odl) {
			return helper.fail({ msg: '文章不存在！' })
		}
		const res = await ctx.service.artical.update(ctx.request.body)
		if (res[0]) {
			helper.success({ msg: '文章修改成功！' })
		} else {
			helper.fail({ msg: '文章修改失败！' })
		}
	}
	async updateList() {
		const { ctx } = this
		const res = await ctx.service.artical.updateList(ctx.request.body.users)
		if (res.flag && res.count) {
			ctx.helper.success({
				msg: '批量添加成功！',
				inserCount: res.count,
			})
		} else {
			ctx.helper.fail({ msg: '批量添加失败！数据重复！' })
		}
	}
	async getArticelById() {
		const { ctx } = this

		const { id } = ctx.params
		console.log(id)
		const res = await ctx.service.artical.getArticelById(id)

		if (res) {
			ctx.helper.success({ data: res })
		}
	}
}
module.exports = ArticalController
